/*
@Time : 2019/6/1 0001 14:44
@Author : visons
*/
package login

import (
	"cellent_test/base/dbDao"
	"cellent_test/proto"
	"cellent_test/proto/code"
	"cellent_test/proto/common"
	"cellent_test/proto/msg"
	"cellent_test/svc/player_svc/playerActor"
	"cellent_test/svc/player_svc/player_manager"
	"github.com/davyxu/cellnet"
	"time"
)

func Init() {
	proto.Handler_LoginREQ = func(ev cellnet.Event) {
		message := ev.Message().(*msg.LoginREQ)
		user := dbDao.GetUserByOpenId(message.OpenId)
		actor := &playerActor.PlayerActor{}
		actor.SetLastPingTime(time.Now())
		actor.SetSession(ev.Session())
		actor.SetUser(user)
		player_manager.GetPlayerManager().PutPlayer(actor)

		pdata := &common.PlayerData{
			Nickname: user.Nickname,
			Avatar:   user.Avatar,
			Gold:     user.Gold,
		}

		ev.Session().Send(&msg.LoginACK{
			Code:       code.ResultCode_Success,
			PlayerData: pdata,
		})
	}
}
